《C++程序设计实践》实验十二

您所在的位置:网站首页 a5与a4的区别 对比 《C++程序设计实践》实验十二

《C++程序设计实践》实验十二

2024-02-13 06:35| 来源: 网络整理| 查看: 265

《C++程序设计实践》实验十二 一、课内实验题(共5小题,50分) 题型得分 50 【描述】 声明并实现一个Student类,表示学生信息。Student类包括: int类型的私有数据成员num,表示学号。 string类型的私有数据成员name,表示姓名。 int类型的私有数据成员score,表示成绩 char类型的私有数据成员grade,表示等级。 无参(默认)构造函数。 有参构造函数,将学号、姓名、成绩和等级设置为给定的参数。 访问器函数getNum、getName、getScore、getGrade,分别用于访问学号、姓名、成绩和等级。 重载流提取运算符>>和流插入运算符 } Student(int num,string name,int score,char grade){ this->num=num; this->name=name; this->score=score; this->grade=grade; }

int getNum() const{ return num; } string getName() const { return name; } int getScore() const{ return score; } char getGrade() const{ return grade; } friend ostream &operator(istream &in, Student &student); private: int num; //学号 string name; //姓名 int score; //成绩 char grade; //等级

}; ostream &operator in >>student.num>>student.name>>student.score>>student.grade; return in; }

int main(){ ofstream outFile; outFile.open(“student.txt”); if(!outFile.is_open()){ cerr case 10: case 9: grade=‘A’; break; case 8: grade=‘B’; break; case 7: grade=‘C’; break; case 6: grade=‘D’; break; default: grade=‘E’; break; } Student student(num,name,score,grade); outFilenum; } outFile.close(); return 0; } 题目得分 10 【描述】 声明并实现了一个Rectangle类,表示矩形。Rectangle类包括: double类型的私有数据成员width和height,表示矩形的宽和高。 带默认参数的构造函数,将矩形的宽和高设置为给定的参数。宽和高的默认参数值为1。 更改器函数setWidth和setHeight,分别用于修改矩形的宽和高。 访问器函数getWidth和getHeight,分别用于访问矩形的宽和高。 成员函数computeArea,返回矩形的面积。 成员函数computePerimeter,返回矩形的周长。 创建5个Rectangle对象(每个Rectangle对象的宽和高分别为1、2、3、4、5)并将它们写入二进制文件object.dat中。修改第3个对象的宽为10、高为3.5,再把修改后的该对象写回文件原位置。 【输入】 没有输入。 【输出】 生成文件object.dat 不需要在屏幕上显示信息。 【来源】 《程序设计基础——以C++为例》第8章实验4。(10分) 我的答案: #include #include #include #include using namespace std;

class Rectangle{ public: Rectangle(double width=1,double height=1){ this->width=width; this->height=height; }

void setWidth(double width){ this->width=width; } void setHeight(double height){ this->height=height; } double getWidth() const{ return width; } double getHeight() const{ return height; } double computeArea() const{ return width*height; } double computePerimeter() const{ return 2*(width+height); }

private: double width; double height; };

int main(){ fstream ioFile; ioFile.open(“object.dat”, ios::in | ios::out | ios::trunc | ios::binary); if(!ioFile.is_open()) { cerr exit(EXIT_FAILURE); } process(inFile); return 0; }

void process(ifstream &inFile) { int total[DAYS] = {0}; int numbers[DAYS] = {0}; int size; while (!inFile.eof()) { string line; getline(inFile, line); size = transferFrom(numbers, line); cout numbers[i] = value; ++i; } for (j = i; j < DAYS; ++j) numbers[j] = 0; return i; }

int sum(int numbers[], int size) { int s = 0; for (int i = 0; i < size; ++i) s += numbers[i]; return s; }

void addTo(int total[], int numbers[]) { for (int i = 0; i < DAYS; ++i) total[i] += numbers[i]; }

void print(int total[]) { string dayNames[] = {“Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”}; int s = 0; for (int i = 0; i < DAYS; ++i) { cout if (id == ids[i]) { found = i; break; } } if (found == -1) ids[user_count++] = id; } fin.close(); cout cerr ch+=2; outFile.put(ch); inFile.get(ch); } inFile.close(); outFile.close(); return 0; } 题目得分 10



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3